<?php
class Object extends CI_Model {
	function addObject($id_User, $name, $type, $address, $url, $id_Location) {
		$data = array ('url' => $url, 'id_Location' => $id_Location, 'id_User' => $id_User, "id_Type" => $type, 'name' => $name, 'address' => $address );
		$this->db->insert ( 'objs', $data );
		return $this->db->insert_id ();
	}
	function updateObject($id_User, $id_Obj, $address, $url, $id_Location) {
		if ($address == '')
			$data = array ('url' => $url, 'id_Location' => $id_Location, 'id_User' => $id_User );
		if ($url == '')
			$data = array ('id_Location' => $id_Location, 'id_User' => $id_User, 'address' => $address );
		if ($id_Location == '')
			$data = array ('url' => $url, 'id_User' => $id_User, 'address' => $address );
		if ($address == '' && $url == '')
			$data = array ('id_Location' => $id_Location, 'id_User' => $id_User );
		if ($address == '' && $id_Location == '')
			$data = array ('url' => $url, 'id_User' => $id_User );
		if ($url == '' && $id_Location == '')
			$data = array ('id_User' => $id_User, 'address' => $address );
		if ($address == '' && $id_Location == '' && $url == '')
			$data = array ('id_User' => $id_User );
		if ($address != '' && $url != '' && $id_Location != '')
			$data = array ('url' => $url, 'id_Location' => $id_Location, 'id_User' => $id_User, 'address' => $address );
		$this->db->where ( 'id_Obj', $id_Obj );
		$this->db->update ( 'objs', $data );
		
		if ($this->db->affected_rows () > 0) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	function search($type, $name, $id_Location) {
		$this->db->select ( '*' );
		$this->db->from ( "objs" );
		//$this->db->where ( 'id_Type', $type );
		//$this->db->where ( 'name', $name );
		$like = $this->db->escape_like_str ( $name );
		if ($type != '')
			$this->db->where ( "(name LIKE  '%$like%' AND id_Type = $type AND id_Location = $id_Location)" );
		else
			$this->db->where ( "(name LIKE  '%$like%' AND id_Location = $id_Location)" );
		$query = $this->db->get ();
		if ($query->num_rows () >= 1) {
			return $query->result ();
		} else {
			return false;
		}
	
	}
	function search2($type, $name) {
		$this->db->select ( '*' );
		$this->db->from ( "objs" );
		//$this->db->where ( 'id_Type', $type );
		//$this->db->where ( 'name', $name );
		$like = $this->db->escape_like_str ( $name );
		$this->db->where ( "(name LIKE  '%$like%' AND id_Type = $type)" );
		$query = $this->db->get ();
		if ($query->num_rows () >= 1) {
			return $query->result ();
		} else {
			return false;
		}
	
	}
	function add($name, $type, $address, $url, $id_Location, $id_Photo) {
	
	}
	function getAllObjectByType($type) {
		
		$this->db->select ( '*' );
		$this->db->from ( 'objs' );
		$this->db->where ( 'id_Type', $type );
		$query = $this->db->get ();
		if ($query->num_rows () >= 1) {
			return $query->result ();
		} else {
			return false;
		}
	}
	function getObject($id_Obj) {
		$this->db->select ( '*' );
		$this->db->from ( 'objs' );
		$this->db->where ( 'id_Obj', $id_Obj );
		$query = $this->db->get ();
		if ($query->num_rows () >= 1) {
			return $query->result ();
		} else {
			return false;
		}
	
	}
	
	function getObjectByLocation($id_Location) {
		$this->db->select ( 'id_Obj' );
		$this->db->from ( 'objs' );
		$this->db->where ( 'id_Location', $id_Location );
		$query = $this->db->get ();
		if ($query->num_rows () >= 1) {
			return $query->result ();
		} else {
			return false;
		}
	}
	function getObjectByName($name) {
		$this->db->select ( '*' );
		$this->db->from ( 'objs' );
		$like = $this->db->escape_like_str ( $name );
		$this->db->where ( "name LIKE  '%$like%'" );
		$query = $this->db->get ();
		if ($query->num_rows () >= 1) {
			return $query->result ();
		} else {
			return false;
		}
	}
}